<?php

class Admin_UserAclController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function indexAction()
    {
    }
    
    public function showRoleListAction()
    {
        $roleType = $this->filter->filter($this->_request->get('roleType'));
        
        $roles = array();
        $_roleAllArray = App_Model_DbTable_Acl_Role::getInstance()->getAllByRole($roleType);
        for($i=0;$i<count($_roleAllArray);$i++)
        {
            $custom = $_roleAllArray[$i]['custom'];
            $_parentRoldId = $_roleAllArray[$i]['parentId'];
            $_parentRoleRow = App_Model_DbTable_Acl_Role::getInstance()->getRowById($_parentRoldId);
            $_roleAllArray[$i]['parentRoleName'] = isset($_parentRoleRow['roleName']) ? $_parentRoleRow['roleName'] : "";
            
            $roles[$custom][] = $_roleAllArray[$i];
        }
        
        $this->view->roleType = $roleType;
        $this->view->roles    = $roles;
    }
    
    public function showRoleAccessListAction()
    {
        $roleId    = $this->filter->filter($this->_request->get('roleId')); 
        $_showType = $this->filter->filter($this->_request->get('showType')); 
        
        $_roleRow = App_Model_DbTable_Acl_Role::getInstance()->getRowById($roleId);
        
        
        $accessAllArray = App_Model_DbTable_Acl_Access::getInstance()->getAll();
        
        $_roleFamilyArray = array();
        $_roleFamilyArray = App_Model_Role::getInstance()->getRoleFamilies($roleId, $_roleFamilyArray);
        sort($_roleFamilyArray);
        for($i=0;$i<count($_roleFamilyArray);$i++)
        {
            $__roleId = $_roleFamilyArray[$i]['id'];
            $roleControlArray[$__roleId]['access'] = array();
            
            $_accessControlArray = array();
            $_roleControlAllArray = App_Model_DbTable_Acl_Control::getInstance()->getControlAllByRoleId($__roleId);
            foreach ($_roleControlAllArray as $key=>$_controlRow)
            {
                $_accessControlArray[$_controlRow['accessId']] = $_controlRow;
            }
            
            for($ii=0;$ii<count($accessAllArray);$ii++)
            {
                $_accessId = $accessAllArray[$ii]['id'];

                if(array_key_exists($_accessId, $_accessControlArray))
                {
                    $_controlRow = $_accessControlArray[$_accessId];
                }
                else 
                {
                    $row = array (
                        'roleId'             => $__roleId,
                        'accessId'             => $_accessId,
                        'controlType'         => 0,
                    );
                    $_controlId = App_Model_DbTable_Acl_Control::getInstance()->addNew($row);
                    
                    $_controlRow = App_Model_DbTable_Acl_Control::getInstance()->getRowById($_controlId);
                }
                
                $_controlRow = self::_buildControlRow($_controlRow);
                $_roleFamilyArray[$i]['control'][] = $_controlRow;
                
                $accessAllArray[$ii]['control'][$i] = $_controlRow;
            }
        }
        
        $accessGroupes = array();
        for($i=0;$i<count($accessAllArray);$i++)
        {
            $access = $accessAllArray[$i];
            
            $accessId       = $access['id'];
            $parentAccessId = $access['parentId'];
            if(!$parentAccessId){
                $accessGroupes[$accessId][] = $access;
            }
        }
        for($i=0;$i<count($accessAllArray);$i++)
        {
            $access = $accessAllArray[$i];
            
            $accessId       = $access['id'];
            $parentAccessId = $access['parentId'];
            if($parentAccessId){
                $accessGroupes[$parentAccessId][] = $access;
            }
        }

        
        $this->view->roleRow  = $_roleRow;
        $this->view->showType = $_showType;
        $this->view->accessGroupes   = $accessGroupes;
        $this->view->roleFamilyArray = $_roleFamilyArray;
    }
    
    public function getRoleAccessForJeditableAction()
    {
        $viewResult = new Ext_View_Result();

        $roleId    = $this->filter->filter($this->_request->getPost('roleId')); 
        $_accessId = $this->filter->filter($this->_request->getPost('accessId')); 
        
        $_optionArray['0'] = ''; 
        $_optionArray['1'] = 'Allow'; 
        $_optionArray['2'] = 'Deny'; 

        $_controlRow = App_Model_DbTable_Acl_Control::getInstance()->getRowByRoleIdAccessId($roleId, $_accessId);
        if($_controlRow)
        {
            $_optionArray['selected'] = $_controlRow['controlType'];
        }

        $viewResult->setNoLayout();
        $viewResult->setNoRender();
        
        echo Zend_Json::encode($_optionArray);
    }
    
    public function saveRoleAccessForJeditableAction()
    {
        $viewResult = new Ext_View_Result();
        
        $_controlType = $this->filter->filter($this->_request->getPost('value')); 
        $roleId       = $this->filter->filter($this->_request->getPost('roleId')); 
        $_accessId    = $this->filter->filter($this->_request->getPost('accessId')); 
        
        $_controlRow = App_Model_DbTable_Acl_Control::getInstance()->getRowByRoleIdAccessId($roleId, $_accessId);
        if($_controlRow)
        {
            $_controlId = $_controlRow['id'];
            
            $row = array (
                'controlType'         => $_controlType,
            );
            App_Model_DbTable_Acl_Control::getInstance()->addOld($row, $_controlId);
        }
        else 
        {
            $row = array (
                'roleId'             => $roleId,
                'accessId'             => $_accessId,
                'controlType'         => $_controlType,
            );
            $_controlId = App_Model_DbTable_Acl_Control::getInstance()->addNew($row);
        }
        
        $_controlRow = App_Model_DbTable_Acl_Control::getInstance()->getRowById($_controlId);
        $_controlRow = self::_buildControlRow($_controlRow);
        echo $_controlRow['controlName'];  
        
        $viewResult->setNoLayout();
        $viewResult->setNoRender();
    }
    
    public function getRoleAccessListAction()
    {
        $viewResult = new Ext_View_Result($this);
        $translate = Zend_Registry::get('Zend_Translate');
        $filter=new Zend_Filter_StripTags();
        $_continue = 1;
        
        $_appModelAcl = Ext_Singleton::getInstance("App_Model_Acl");
        $_appModelRole = Ext_Singleton::getInstance("App_Model_Role");
        $_appModelDbTableAclAccess = Ext_Singleton::getInstance("App_Model_DbTable_Acl_Access");
        $_appModelDbTableAclControl = Ext_Singleton::getInstance("App_Model_DbTable_Acl_Control");
        $_appModelDbTableAclRole = Ext_Singleton::getInstance("App_Model_DbTable_Acl_Role");
        
        $_roleAccessArray = array();
        $filter=new Zend_Filter_StripTags();
        $roleId = $filter->filter($this->_request->get('roleId')); 
        
        $roleRow = $_appModelDbTableAclRole->getRowById($roleId);
        
        $roleControlAllArray = $_appModelDbTableAclControl->getControlAllByRoleId($roleId);
        $roleAccessControlArray = array();
        foreach ($roleControlAllArray as $key=>$_controlRow)
        {
            $_controlRow = self::_buildControlRow($_controlRow);
            $roleAccessControlArray[$_controlRow['accessId']] = $_controlRow;
        }
        
        $accessAllArray = $_appModelDbTableAclAccess->getAll();
        
        $_roleFamilyArray = array();
        $_roleFamilyArray = $_appModelRole->getRoleFamilyArray($roleId, $_roleFamilyArray);
        for($i=0;$i<count($_roleFamilyArray);$i++)
        {
            $__roleId = $_roleFamilyArray[$i]['id'];
            $roleControlAllArray = $_appModelDbTableAclControl->getControlAllByRoleId($__roleId);
            $roleAccessControlArray = array();
            foreach ($roleControlAllArray as $key=>$_controlRow)
            {
                $_controlRow = self::_buildControlRow($_controlRow);
                $roleAccessControlArray[$_controlRow['accessId']] = $_controlRow;
            }
            
            foreach ($accessAllArray as $key=>$_accessRow)
            {
                $_accessId = $_accessRow['id'];
                if(array_key_exists($_accessId, $roleAccessControlArray))
                {
                    $_accessRow['controlName'] = $roleAccessControlArray[$_accessId]['controlName'];
                }
                else 
                {
                    $_accessRow['controlName'] = "&nbsp;";
                }
                
                $_roleFamilyArray[$i]['access'][] = $_accessRow;
            }
        }
        
        $this->view->roleRow = $roleRow;
        $this->view->accessAllArray = $accessAllArray;
        $this->view->roleFamilyArray = $_roleFamilyArray;
        
        $viewResult->setNoLayout();
    }
    
    public function showRoleFormAction()
    {
        $roleType = $this->filter->filter($this->_request->get('roleType'));
        $roleId   = $this->filter->filter($this->_request->get('roleId'));
        
        if($roleId)
        {
            $role = App_Model_DbTable_Acl_Role::getInstance()->getRowById($roleId);
            $this->view->role = $role;
            $roleType = $role['roleType'];
        }
        
        $viewForm = new Ext_View_Form_Admin_Acl_Role();
        
        if(isset($role['id']))
        {
            $viewForm->getElement('roleId')->setValue($role['id']);
        }
        
        if(isset($role['roleName']))
        {
            $viewForm->getElement('roleName')->setValue($role['roleName']);
        }
        
        $options = array();
        $options['guest']  = 'guest';
        $options['member'] = 'member';
        $options['admin']  = 'admin';
        $viewForm->getElement('roleType')->addMultiOptions($options);
        $viewForm->getElement('roleType')->setValue($roleType);
        
        $roleAll = App_Model_DbTable_Acl_Role::getInstance()->getAll();
        $options = array();
        for($i=0;$i<count($roleAll);$i++)
        {
            $_roleId = $roleAll[$i]['id'];
            if($_roleId == $roleId)
            {
                continue;
            }

            if($roleId){
                $roleFamilyIdArray = App_Model_Role::getInstance()->getRoleFamilyIds($_roleId);
                if(in_array($roleId, $roleFamilyIdArray))
                {
                    continue;
                }
            }
            
            $options[$_roleId] = $roleAll[$i]['roleName'];
        }
        $viewForm->getElement('parentId')->addMultiOption(null, null);
        $viewForm->getElement('parentId')->addMultiOptions($options);
        if(isset($role['parentId'])){
            $viewForm->getElement('parentId')->setValue($role['parentId']);
        }

        if(isset($role['description']))
        {
            $viewForm->getElement('description')->setValue($role['description']);
        }
        
        $this->view->form = $viewForm;
    }
    
    public function saveRolePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $roleId      = $this->filter->filter($this->_request->getPost('roleId'));
        $roleName    = $this->filter->filter($this->_request->getPost('roleName'));
        $roleType    = $this->filter->filter($this->_request->getPost('roleType'));
        $parentId    = $this->filter->filter($this->_request->getPost('parentId'));
        $description = $this->filter->filter($this->_request->getPost('description'));
            
        $row = array(
            'roleName'    => $roleName,
            'roleType'    => $roleType,
            'parentId'    => $parentId,
            'description' => $description,
        );
        if($roleId){
            App_Model_DbTable_Acl_Role::getInstance()->addOld($row, $roleId);
        }else{
            $row['custom'] = 1;
            $roleId = App_Model_DbTable_Acl_Role::getInstance()->addNew($row);
        }
        
        $viewResult->addMessage($this->translater->_('Submit success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function deleteRoleAction()
    {
        $viewResult = new Ext_View_Result();
        
        $roleId = $this->filter->filter($this->_request->getPost('roleId'));
        if(!$roleId){
            $viewResult->addMessage("Role id is not found.");
        }
        $role = App_Model_DbTable_Acl_Role::getInstance()->getRowById($roleId);
        if(!$roleId){
            $viewResult->addMessage("Role record is not found.");
        }
        if($role['custom'] != 1){
            $viewResult->addMessage("Only defined role can be delete.");
        }
        
        App_Model_DbTable_Acl_Role::getInstance()->del($roleId);
        $viewResult->addMessage($this->translater->_('Submit success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    private function _buildControlRow($controlRow)
    {
        if($controlRow['controlType'] == 1)
        {
            $controlRow['controlName'] = "allow";
        }
        elseif($controlRow['controlType'] == 2)
        {
            $controlRow['controlName'] = "deny";
        }
        else 
        {
            $controlRow['controlName'] = "";
        }
        
        return $controlRow;
    }
}

